﻿using MeterReader.PubSubEvents;
using MeterReader.WPF.Services;

namespace MeterReader.WPF.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;
        private readonly IReadData _dataService;

        public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IReadData dataService)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            _dataService = dataService;
			_dataService.WorkStatusChanged += WorkStatusChanged;
			_regionManager.RegisterViewWithRegion("MainRegion", "MetersDataMonitor");
            _eventAggregator.GetEvent<NavigateEvent>().Subscribe(NavigateTo);
            StopAppCommand = new(StopApp);
        }

		private void WorkStatusChanged(object? sender, EventArgs e)
		{
			IsWorking = _dataService.IsWorking;
			RaisePropertyChanged(nameof(IsWorking));
		}

		private void NavigateTo(string obj) => _regionManager.RequestNavigate("MainRegion", obj);

        private void StopApp() => _dataService.StopRead();

        public DelegateCommand StopAppCommand { get; set; }

        private bool _isWorking;
        public bool IsWorking
        {
            get { return _isWorking; }
            set { SetProperty(ref _isWorking, value); }
        }
    }
}
